package com.td.module.system.controller.admin.growth;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.system.controller.admin.growth.vo.data.*;
import com.td.module.system.convert.growth.GrowthModuleDataConvert;
import com.td.module.system.dal.dataobject.growth.GrowthModuleDataDO;
import com.td.module.system.service.growth.GrowthModuleDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 生长周期")
@RestController
@RequestMapping("/growth/module-data")
@Validated
public class GrowthModuleDataController {

    @Resource
    private GrowthModuleDataService moduleDataService;

    @PostMapping("/create")
    @ApiOperation("创建生长周期")
    @PreAuthorize("@ss.hasPermission('growth:module-data:create')")
    public CommonResult<Long> createModuleData(@Valid @RequestBody GrowthModuleDataCreateReqVO createReqVO) {
        return success(moduleDataService.createModuleData(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新生长周期")
    @PreAuthorize("@ss.hasPermission('growth:module-data:update')")
    public CommonResult<Boolean> updateModuleData(@Valid @RequestBody GrowthModuleDataUpdateReqVO updateReqVO) {
        moduleDataService.updateModuleData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除生长周期")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('growth:module-data:delete')")
    public CommonResult<Boolean> deleteModuleData(@RequestParam("id") Long id) {
        moduleDataService.deleteModuleData(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得生长周期")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<GrowthModuleDataRespVO> getModuleData(@RequestParam("id") Long id) {
        GrowthModuleDataDO moduleData = moduleDataService.getModuleData(id);
        return success(GrowthModuleDataConvert.INSTANCE.convert(moduleData));
    }

    @GetMapping("/list")
    @ApiOperation("获得生长周期列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<GrowthModuleDataRespVO>> getModuleDataList(@RequestParam("ids") Collection<Long> ids) {
        List<GrowthModuleDataDO> list = moduleDataService.getModuleDataList(ids);
        return success(GrowthModuleDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得生长周期分页")
    public CommonResult<PageResult<GrowthModuleDataRespVO>> getModuleDataPage(@Valid GrowthModuleDataPageReqVO pageVO) {
        PageResult<GrowthModuleDataDO> pageResult = moduleDataService.getModuleDataPage(pageVO);
        return success(GrowthModuleDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出生长周期 Excel")
    @PreAuthorize("@ss.hasPermission('growth:module-data:export')")
    @OperateLog(type = EXPORT)
    public void exportModuleDataExcel(@Valid GrowthModuleDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<GrowthModuleDataDO> list = moduleDataService.getModuleDataList(exportReqVO);
        // 导出 Excel
        List<GrowthModuleDataExcelVO> datas = GrowthModuleDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生长周期.xls", "数据", GrowthModuleDataExcelVO.class, datas);
    }

}
